package com.njtoyo.taxi.admin.service.data.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.njtoyo.taxi.admin.library.Const;
import com.njtoyo.taxi.admin.logic.allowance.DriverRewardLogic;
import com.njtoyo.taxi.admin.logic.allowance.OrderLogic;
import com.njtoyo.taxi.admin.logic.allowance.RecommendationLogic;
import com.njtoyo.taxi.admin.mapper.master.enums.DriverRewardCategory;
import com.njtoyo.taxi.admin.mapper.master.mapper.CouponMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.CustomerIncomeMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.RideTransactionLogMapper;
import com.njtoyo.taxi.admin.rest.presenter.data.allowance.*;
import com.njtoyo.taxi.admin.rest.wrapper.data.allowance.AllowanceChartWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.data.allowance.AllowanceWrapper;
import com.njtoyo.taxi.admin.service.data.AllowanceService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@DS(Const.PRIMARY_DS)
public class AllowanceServiceImpl implements AllowanceService {

    @Autowired
    private RecommendationLogic recommendationLogic;

    @Autowired
    private OrderLogic orderLogic;

    @Autowired
    private DriverRewardLogic driverRewardLogic;

    @Autowired
    private RideTransactionLogMapper rideTransactionLogMapper;

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private CustomerIncomeMapper customerIncomeMapper;

    @Override
    public RestResult<AllowanceStatsPresenter> main(AdminUser adminUser, AllowanceWrapper wrapper) {
        AllowanceStatsPresenter res = new AllowanceStatsPresenter();

        // 推广人数
        res.setRecommendCount(recommendationLogic.getCount(wrapper.getStartTime().toString(),
                wrapper.getEndTime().toString()).longValue());
        // 订单数
        res.setOrderCount(orderLogic.getRideOrderCount(wrapper.getStartTime(), wrapper.getEndTime()).longValue());
        res.setScanCount(orderLogic.getScanRideOrderCount(wrapper.getStartTime(), wrapper.getEndTime()).longValue());
        // 司机补贴
        List<DriverRewardCategory> rewardCategory = new ArrayList<>();
        rewardCategory.add(DriverRewardCategory.redPacket);
        rewardCategory.add(DriverRewardCategory.onlineOrder);
        res.setDriverAllowance(driverRewardLogic.getAmountByCategory(wrapper.getStartTime(), wrapper.getEndTime(), rewardCategory));
        // 司机推广补贴
        rewardCategory.clear();
        rewardCategory.add(DriverRewardCategory.recommend);
        rewardCategory.add(DriverRewardCategory.recommendation_first_order_reward);
        res.setDriverRecommend(driverRewardLogic.getAmountByCategory(wrapper.getStartTime(), wrapper.getEndTime(), rewardCategory));

        // 乘客补贴
        String startTime = DateUtil.formatDateTime(wrapper.getStartTime());
        String endTime = DateUtil.formatDateTime(wrapper.getEndTime());
        // 优惠券、余额、波浪
        CustomerProportionPresenter customerAllowance = rideTransactionLogMapper.getTransactionAmount(startTime, endTime);
        // 小程序
        CustomerTotalPresenter hadUsed = customerIncomeMapper.selectTotalAllowance(startTime, endTime);
        res.setCustomerAllowance(BigDecimal.valueOf(0).add(hadUsed.getTotalValue())
                .add(customerAllowance.getCouponAmount())
                .add(customerAllowance.getBalanceAmount())
                .add(customerAllowance.getAllowanceAmount()));
        // 乘客推广补贴
        CustomerTotalPresenter recommendationReduction = couponMapper.selectRecommendationAllowanceForReduction(startTime, endTime);
        CustomerTotalPresenter recommendationDiscount = couponMapper.selectRecommendationAllowanceForDiscount(startTime, endTime);
        BigDecimal recommendationValue = NumberUtil.add(recommendationReduction.getTotalValue(), recommendationDiscount.getTotalValue());
        res.setCustomerRecommend(recommendationValue);

        return RestResult.success(res);
    }

    @Override
    public RestResult<List<OverviewAllowanceChartPresenter>> totalChart(AdminUser adminUser, AllowanceChartWrapper wrapper) {
        return null;
    }

    @Override
    public RestResult<List<OverviewRecommendChartPresenter>> recommendChart(AdminUser adminUser, AllowanceChartWrapper wrapper) {
        return null;
    }
}
